module.exports = function ($scope, $state, $resource, $modal, toaster ){
    //查询法律法规列表
    $scope.search = function (pageNo){
      
        if($scope.date.lable){
            if(!$scope.date1.lable){
                alert('开始和结束日期必须同时输入进行搜索')
                return;
            }
        }
        if($scope.date1.lable){
            if(!$scope.date.lable){
                alert('开始和结束日期必须同时输入进行搜索')
                return;
            }
        }
        if($scope.date.lable == undefined && $scope.date1.lable == undefined){
            var para = {
                'pageNo': pageNo || $scope.currentPage,
                'pageSize': $scope.itemsPerPage,
                'law_title': $scope.info.law_title
                
            }
        }else{
            var para = {
                'pageNo': pageNo || $scope.currentPage,
                'pageSize': $scope.itemsPerPage,
                'law_title': $scope.info.law_title,
                'start': date2str($scope.date.lable) + ' 00:00:00',
                'end' : date2str($scope.date1.lable) + ' 23:59:59'
                
            }
        }
        
        $resource('/api/as/goc/pecompanyrules/findRuleList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.list = res.data.results;
                $scope.totalItems = res.data.totalRecord;

                $scope.currentPage = pageNo;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
    function date2str(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

    $scope.date = {
        //  'lable': date2str(new Date()),
        //  'value': date2str(new Date()),
    // 'lable': date2str2(new Date()),
            'opened': false
        }


         $scope.dateOpen = function ($event, item) {
            $event.preventDefault();
            $event.stopPropagation();
            item.opened = true;
        };

        $scope.date1 = {
            //   'lable': date2str(new Date()),
            //  'value': date2str(new Date()),
            'opened': false
        }


         $scope.dateOpen1 = function ($event, item) {
            $event.preventDefault();
            $event.stopPropagation();
            item.opened = true;
        };
         


    init();
    //初始化列表
    function init(){
        $scope.currentPage = 1;		//当前页码
        $scope.itemsPerPage = 10;	//每页显示几条
        $scope.searchform = {};
        $scope.search(1);
    }

    //添加or修改
    $scope.saveRulesInfo = function(info) {
        var modalInstance = $modal.open({
            template: require('../views/saveRulesInfo.html'),
            controller: 'saveRulesInfo',
            size: 'lg',
            resolve: {
                item : function () {
                    return info;
                },
                flag : function () {
                    return info == 1;
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function() {
            init();
        });
    }

    //查看
    $scope.gatRulesInfo = function(info) {
        info.openstate = 'true';
      
        var modalInstance = $modal.open({
            template: require('../views/saveRulesInfo.html'),
            controller: 'saveRulesInfo',
            size: 'lg',
            resolve: {
                item : function () {
                    return info;
                },
                flag : function () {
                    return info == 1;
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function() {
            init();
        });
    }



    //删除
    $scope.delete = function (info){
        var para = {
            'id' : info.id
        }
        $resource('/api/as/goc/pecompanyrules/del', {}, {}).get(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '删除成功'});
                init();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //发布
    $scope.updateup = function (info){
        var para = {
            'id' : info.id
        }
        $resource('/api/as/goc/pecompanyrules/updateup', {}, {}).get(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '发布成功'});
                init();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //停止发布
    $scope.updatedown = function (info){
        var para = {
            'id' : info.id
        }
        $resource('/api/as/goc/pecompanyrules/updatedown', {}, {}).get(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '停止发布成功'});
                init();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

};